class TextWedge extends Wedge {
    constructor({
        text,
        fontSize = 16,
        fontFamily = 'Arial',
        fontStyle = 'normal',
        color = '#000',
        textAlign = 'left',
        textBaseline = 'top',
        ...options
    }) {
        super({ ...options, type: 'text' });
        this.text = text;
        this.fontSize = fontSize;
        this.fontFamily = fontFamily;
        this.fontStyle = fontStyle;
        this.color = color;
        this.textAlign = textAlign;
        this.textBaseline = textBaseline;
        this.calculateTextSize();

        // 更新父类的旋转中心
        this.rotationCenterX = this.width / 2;
        this.rotationCenterY = this.height / 2;
    }

    // 获取文本内容
    getText() {
        return this.text;
    }

    // 设置文本内容，并重新计算文本大小
    setText(newText) {
        this.text = newText;
        this.calculateTextSize();
    }

    // 设置字体大小，并重新计算文本大小
    setFontSize(fontSize) {
        this.fontSize = fontSize;
        this.calculateTextSize();
    }

    // 设置字体家族
    setFontFamily(fontFamily) {
        this.fontFamily = fontFamily;
        this.calculateTextSize();
    }

    // 设置字体样式
    setFontStyle(fontStyle) {
        this.fontStyle = fontStyle;
        this.calculateTextSize();
    }

    // 设置颜色
    setColor(color) {
        this.color = color;
    }

    // 设置文本对齐方式
    setTextAlign(textAlign) {
        this.textAlign = textAlign;
    }

    // 设置文本基线
    setTextBaseline(textBaseline) {
        this.textBaseline = textBaseline;
    }

    // 整体设置属性方法
    setAttributes(attributes) {
        if (Object.prototype.hasOwnProperty.call(attributes, 'text')) {
            this.text = attributes.text;
        }
        if (Object.prototype.hasOwnProperty.call(attributes, 'fontSize')) {
            this.setFontSize(attributes.fontSize);
        }
        if (Object.prototype.hasOwnProperty.call(attributes, 'fontFamily')) {
            this.setFontFamily(attributes.fontFamily);
        }
        if (Object.prototype.hasOwnProperty.call(attributes, 'fontStyle')) {
            this.setFontStyle(attributes.fontStyle);
        }
        if (Object.prototype.hasOwnProperty.call(attributes, 'color')) {
            this.setColor(attributes.color);
        }
        if (Object.prototype.hasOwnProperty.call(attributes, 'textAlign')) {
            this.setTextAlign(attributes.textAlign);
        }
        if (Object.prototype.hasOwnProperty.call(attributes, 'textBaseline')) {
            this.setTextBaseline(attributes.textBaseline);
        }
    }

    calculateTextSize() {
        const canvas = document.createElement('canvas');
        const context = canvas.getContext('2d');
        const font = `${this.fontStyle} ${this.fontSize}px ${this.fontFamily}`;
        context.font = font;
        const lines = this.text.split('\n');
        const textWidth = Math.max(...lines.map((line) => context.measureText(line).width));

        let lineHeight = this.getLineHeight();
        const totalHeight = lines.length * lineHeight;

        const padding = this.getPadding();
        this.width = textWidth + 2 * padding;
        this.height = totalHeight + 2 * padding;
    }

    getLineHeight() {
        return this.fontSize * 1.2;
    }

    getPadding() {
        return this.fontSize * 0.3;
    }

    updateFontSize() {
        const padding = this.getPadding();
        this.fontSize = this.estimateFontSize(this.text, this.width - 2 * padding);
        const lines = this.text.split('\n');
        let lineHeight = this.getLineHeight();
        const totalHeight = lines.length * lineHeight;
        this.height = totalHeight + 2 * padding;
    }

    _render(context) {
        // 构建 font 字符串
        const font = `${this.fontStyle} ${this.fontSize}px ${this.fontFamily}`;
        context.font = font;
        context.fillStyle = this.color;
        context.textAlign = this.textAlign;
        context.textBaseline = this.textBaseline;

        const lines = this.text.split('\n');
        const lineHeight = this.getLineHeight();
        const totalHeight = lines.length * lineHeight;

        const padding = this.getPadding();

        for (let i = 0; i < lines.length; i++) {
            let x, y;

            // 根据对象的对齐方式和文本的对齐方式计算绘制位置
            switch (this.textAlign) {
                case 'center':
                    x = this.x + this.width / 2;
                    break;
                case 'right':
                    x = this.x + this.width - padding;
                    break;
                default:
                    x = this.x + padding;
            }

            switch (this.textBaseline) {
                case 'middle':
                    y = this.y + this.height / 2 - totalHeight / 2 + i * lineHeight + lineHeight / 2;
                    break;
                case 'bottom':
                    y = this.y + this.height - totalHeight + i * lineHeight;
                    break;
                default:
                    y = this.y + padding + i * lineHeight;
            }

            context.fillText(lines[i], x, y);
        }
    }
    estimateFontSize(text, maxWidth) {
        // 预估每种类型字符的大致宽度比例（相对于字体大小）
        const charWidthRatios = {
            hanzi: 1.0, // 汉字宽度约为字体大小的1倍
            ascii: 0.5, // 英文字母宽度约为字体大小的0.5倍
            cjkPunct: 1.0, // 中文标点宽度约为字体大小的1倍
            asciiPunct: 0.3, // 英文标点宽度约为字体大小的0.3倍
        };

        // 正则表达式匹配不同类型的字符
        const hanziRegex = /[\u4e00-\u9fa5]/g;
        const asciiRegex = /[A-Za-z0-9]/g;
        const cjkPunctRegex = /[，。！？：；]/g;
        const asciiPunctRegex = /[.,!?;:]/g;

        // 分割文本为多行
        const lines = text.split('\n');
        let maxLineWidth = 0;

        lines.forEach((line) => {
            // 计算每种类型字符的数量
            const hanziCount = (line.match(hanziRegex) || []).length;
            const asciiCount = (line.match(asciiRegex) || []).length;
            const cjkPunctCount = (line.match(cjkPunctRegex) || []).length;
            const asciiPunctCount = (line.match(asciiPunctRegex) || []).length;

            // 计算预估总宽度
            const estimatedLineWidth =
                hanziCount * charWidthRatios.hanzi +
                asciiCount * charWidthRatios.ascii +
                cjkPunctCount * charWidthRatios.cjkPunct +
                asciiPunctCount * charWidthRatios.asciiPunct;

            // 更新最大行宽
            if (estimatedLineWidth > maxLineWidth) {
                maxLineWidth = estimatedLineWidth;
            }
        });

        // 根据最大行宽和给定的最大宽度来估算字体大小
        const estimatedFontSize = Math.floor(maxWidth / maxLineWidth);

        return estimatedFontSize;
    }
}
